home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / asyam.zip / ASYNC.C < prev    next >
C/C++ Source or Header  |  1993-06-24  |  3KB  |  116 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dos.h>
  4. #include <mem.h>
  5. #include <alloc.h>
  6. #include <conio.h>
  7.  
  8. #include "async.h"
  9.  
  10. int     fifo_enabled = 0;
  11.  
  12. int     UART_ports[] = {0x3f8, 0x2f8, 0x3e8, 0x2e8};
  13. int     UART_interrupts[] = {0xC, 0xB, 0xC, 0xB};
  14. int     UART_onmask[] = {0xEF, 0xF7, 0xEF, 0xF7};
  15. int     UART_offmask[] = {0x10, 0x08, 0x10, 0x08};
  16.  
  17. int     MSRStatus[NUMBEROFPORTS];
  18. int     LSRStatus[NUMBEROFPORTS];
  19.  
  20. struct  async_portS async_port[4];
  21.  
  22. int     open_port(int comport, long baudrate, int parity, int stopbits, int wordlength)
  23. {
  24.     int     lcr = 0;
  25.     int     mcr;
  26.  
  27.     if (async_port[comport].port_open) {
  28.         return (0);
  29.     }
  30.  
  31.     // Set port open status
  32.     async_port[comport].port_open = 1;
  33.  
  34.     // Set handshaking to none
  35.     async_port[comport].handshaking = HSHAKE_NONE;
  36.  
  37.     // Set up transmit buffer
  38.     async_port[comport].txbuf = malloc(TXBUFSIZE);
  39.     memset(async_port[comport].txbuf, 0, TXBUFSIZE);
  40.     async_port[comport].txhead = 0;
  41.     async_port[comport].txtail = 0;
  42.     async_port[comport].txbuflength = 0;
  43.  
  44.     // Set up receive buffer
  45.     async_port[comport].rxbuf = malloc(RXBUFSIZE);
  46.     memset(async_port[comport].rxbuf, 0, RXBUFSIZE);
  47.     async_port[comport].rxhead = 0;
  48.     async_port[comport].rxtail = 0;
  49.     async_port[comport].rxbuflength = 0;
  50.  
  51.     // Set up interrupt handler
  52.     init_ISR(comport);
  53.  
  54.     // Set UART Baud Rate -- Have the function at hand, why not use it?
  55.     set_baudrate(comport, baudrate);
  56.  
  57.     // Set UART LCR -- Faster to do it in one shot, than call set_this and set_that.
  58.     lcr = (wordlength-5) + ((stopbits-1)*4) + parity;
  59.     outportb(UART_ports[comport]+LCR, lcr);
  60.  
  61.     // Setup MCR
  62.     mcr = inportb(UART_ports[comport]+MCR);
  63.     mcr |= MCR_DTR;
  64.     mcr |= MCR_RTS;
  65.     outportb(UART_ports[comport]+MCR, mcr);
  66.  
  67.     // Find out what kind of UART this is, set up fifo if its a 16550A
  68.     async_port[comport].uart_type = async_detect_uart(comport);
  69.     if (async_port[comport].uart_type == UART_16550A) {
  70.         init_fifo(comport, 1);
  71.     }
  72.  
  73.     return (1);
  74. }
  75.  
  76. int     close_port(int comport, int rts, int dtr)
  77. {
  78.     int     mcr;
  79.  
  80.     if (!async_port[comport].port_open) {
  81.         return (0);
  82.     }
  83.  
  84.     deinit_ISR(comport);
  85.  
  86.     // Reset MCR
  87.     mcr = inport(UART_ports[comport]+MCR);
  88.     if (!dtr) {
  89.         mcr ^= MCR_DTR;
  90.     }
  91.     if (!rts) {
  92.         mcr ^= MCR_RTS;
  93.     }
  94.     outportb(UART_ports[comport]+MCR, mcr);
  95.  
  96.     // Disable Fifo
  97.     if (fifo_enabled) {
  98.         outportb(UART_ports[comport]+2, 0);
  99.     }
  100.  
  101.     // Free transmit buffer
  102.     free(async_port[comport].txbuf);
  103.     async_port[comport].txhead = 0;
  104.     async_port[comport].txtail = 0;
  105.  
  106.     // Free receive buffer
  107.     free(async_port[comport].rxbuf);
  108.     async_port[comport].rxhead = 0;
  109.     async_port[comport].rxtail = 0;
  110.  
  111.     // Set port open status to not open
  112.     async_port[comport].port_open = 0;
  113.  
  114.     return (1);
  115. }
  116.